import time
from common.read import ConfigReader
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from business.login_test import TestCaseLogin as Login
base = ConfigReader('config.ini')
base_path = base.get_options("dev_base_url")
# print(base_path) # http://39.107.116.29:8899
class API_Perform(HttpRunner):
    config = Config("testcase description").base_url(base_path).verify(False)
    teststeps = [
        Step(
            RunTestCase("登录")
            .call(Login)
            .export(*["token",])
        )
    ]
"""
项目权限-电商：
管理员
用户
游客
商家
...
"""
def rest_token(token=Login):
    API_Perform.teststeps.clear()
    API_Perform.teststeps.append(
        Step(
            RunTestCase("登录")
                .call(token)
                .export(*["token", ])
        )
    )
headers = {"Authori-zation":"Bearer $token"}

def requests_method(requests_name,method,requests_path,requests_body=None,requests_headers=headers):
    if method =="get" or method =="GET":
        return RunRequest(requests_name)\
            .get(requests_path)\
            .with_headers(**requests_headers)\
            .extract()\
            .with_jmespath("body","res_data") # todo：为了后面参数关联去使用
    elif method == "post" or method =="POST":
        return RunRequest(requests_name)\
            .post(requests_path)\
            .with_headers(**requests_headers)\
            .with_json(requests_body)\
            .extract()\
            .with_jmespath("body","res_data")
    elif method == "put" or method =="PUT":
        return RunRequest(requests_name)\
            .put(requests_path)\
            .with_headers(**requests_headers)\
            .with_json(requests_body)\
            .extract()\
            .with_jmespath("body","res_data")
    elif method == "delete" or method =="DELETE":
        return RunRequest(requests_name)\
            .delete(requests_path)\
            .with_headers(**requests_headers)\
            .extract()\
            .with_jmespath("body","res_data")

def Step_requests(obj): # obj --->requests_method
    return Step(
        obj
    )

def get_last_response(obj): # obj ---->Step_requests
    API_Perform.teststeps.extend(obj) # 将一个列表的值追加到这个列表上
    admin = API_Perform()
    admin.test_start()
    time.sleep(1)
    res_data = admin.with_export(['token',"res_data"]).get_export_variables()
    return res_data































